<?php

/**
 * uniBoard Resend Activation Mail Page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_code_mail{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our User API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Our ReCaptcha
	 *
	 * @var object
	 */
	
	private $recaptcha		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language File
		$appCore -> loadLanguageFile( 'users');
				
		// User Logged In?
		if ( $appCore -> user['id'] > 0)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_users'], $appCore -> lang['resend_activation_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents();
		}
		else if ( $appCore -> banned_ip)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_ip'], $appCore -> lang['resend_activation_title'], 0);
			
			// Run Events
			$appCore -> output -> drawEvents( true);
		}
		else if ( $appCore -> _REQ('done') == 1)
		{
			// Set Event
			$appCore -> output -> setEvent( $appCore -> lang['user_activate_code_sent'], $appCore -> lang['resend_activation_title'], 1);
			
			// Run Events
			$appCore -> output -> drawEvents( true, false);
		}
		else
		{	
			// Block login
			$appCore -> output -> show_login = false;
			
			// Start ReCAPTCHA?
			if ( $appCore -> conf['recaptcha_on_register'])
			{
				$this -> recaptcha = new class_recaptcha();
			}
						
			// Update?
			if ( $appCore -> _POST( 'send') == 1)
			{
				try
				{					
					// Captcha is invalid?
					if ( $this -> appCore -> conf['recaptcha_on_register'] && !$this -> recaptcha -> recaptcha_check())
					{
						throw new exc_small( $this -> appCore -> lang['recaptcha_incorrect_try_again'], $this -> appCore -> lang['recaptcha_incorrect']);
					}
					
					// Request is invalid?
					if ( !$this -> appCore -> validRequest())
					{
						throw new exc_small( $this -> appCore -> lang['wrong_action_auth'], $this -> appCore -> lang['wrong_action_auth_title']);
					}
					
					// Get User Login and EMail
					$user_login = $appCore -> _POST( 'user_login');
					$user_email = $appCore -> _POST( 'user_email');
					
					// One of them is empty?
					if ( !isset($user_login[0]) || !isset($user_email[0]))
					{
						throw new exc_small( $this -> appCore -> lang['user_cp_fields_all'], $this -> appCore -> lang['resend_activation_title']);
					}
					
					// Find user?
					$this -> appCore -> db -> query( 'SELECT user_id, user_name, user_login, user_activation, user_code, user_email, user_language
					FROM ' . DB_PREFIX . 'users
					WHERE user_login LIKE \'' . $this -> appCore -> db -> strEscape( $user_login, true) . '\' AND user_email LIKE \'' . $this -> appCore -> db -> strEscape( $user_email, true) . '\' AND user_id > \'0\'');
					
					// User found?
					if ( $user_result = $this -> appCore -> db -> fetch_array())
					{
						// User already activated?
						if ( $user_result[3] == 0)
						{
							throw new exc_small( $this -> appCore -> lang['user_activate_0'], $this -> appCore -> lang['resend_activation_title']);	
						}
						
						// User activation by admin
						if ( $user_result[3] == 2)
						{
							throw new exc_small( $this -> appCore -> lang['user_activate_2'], $this -> appCore -> lang['resend_activation_title']);	
						}
								
						// Redirect user
						$appCore -> setRedirect( $appCore -> niceLink( 'act=code_mail&done=1', $appCore -> lang['resend_activation_title']), false);
					
						// Keep Script Alive
						$appCore -> stayAlive();
						
						// Start our awesome Mailer
						$mailer = new class_mailer( $appCore);
						
						// Send User E-Mail
						$mailer -> sendEmail(
							$user_result[5],
							$user_result[1],
							$this -> appCore -> lang['email_activation_title'],
							class_strings::parseMailText( file_get_contents( CACHE_PATH . 'lang_' . $user_result[6] . '/email_resend_' . ($user_result[3] == 0 ? 1 : 0) . '.txt'), array(
								'{USER_NAME}' 						=> $user_result[1],
								'{USER_LOGIN}' 						=> $user_result[2],
								'{USER_ACTIVATION_CODE}' 			=> $user_result[4],
								'{BOARD_NAME}' 						=> $this -> appCore -> conf['board_name'],
								'{BOARD_ACTIVATION_URL}' 			=> $this -> appCore -> emailLink( 'act=activate&user=' . $user_result[0] . '&code=' . $user_result[4], $this -> appCore -> lang['board_nav_activation']),
								'{BOARD_ACTIVATION_MANUAL_URL}' 	=> $this -> appCore -> emailLink( 'act=activate&user=' . $user_result[0], $this -> appCore -> lang['board_nav_activation']),
								'{BOARD_URL}' 						=> $this -> appCore -> emailLink()
							)));
					}
					else
					{
						// Not Found
						throw new exc_small( $this -> appCore -> lang['api_user_not_found_data'], $this -> appCore -> lang['resend_activation_title']);
					}
					
					// Free Result
					$this -> appCore -> db -> freeResult();
				}
				catch ( exc_small $exception)
				{
					// Set Error
					$this -> appCore -> output -> setEvent( $exception -> message, $exception -> title, 0);
			
					// Throw us back to form
					$this -> drawForm();
				}
			}
			else
			{
				// Draw Form
				$this -> drawForm();
			}
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Manual Activation Form
	 *
	 */
	
	function drawForm(){
				
		// Load Templates
		$templates = new tpl_register_page( $this -> appCore);
				
		// Template values
		$tpl_vars = array(
			'act_code' => $this -> appCore -> session -> session_code,
			'act_key' => $this -> appCore -> session -> session_key,
			'user_login' => $this -> appCore -> _POST( 'user_login'),
			'user_email' => $this -> appCore -> _POST( 'user_email')
		);
		
		// Show reCAPTCHA?
		if ( $this -> appCore -> conf['recaptcha_on_register'])
		{
			$tpl_vars['show_captcha'] = $this -> appCore -> session -> session_code;
			$tpl_vars['captcha_html'] = $this -> recaptcha -> recaptcha_get_html( $this -> appCore);
		}
		
		// Draw Template
		$this -> appCore -> output -> draw( $this -> appCore -> lang['resend_activation_title'], array(
			array( $this -> appCore -> niceLink( 'act=code_mail', $this -> appCore -> lang['resend_activation_title']),  $this -> appCore -> lang['resend_activation_title'])
		), $templates -> tpl_activation_send_form( $tpl_vars));
	
	}
	
}